package com.janko.redislock.io.nettyTest;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @Author janko
 * @Version 1.0
 * @date 2021/3/28 21:47
 * @Description 类文件说明
 */
public class NettyClient {

    //启动一个netty的客户端程序
    public static void main(String[] args) {
        try {
            NioEventLoopGroup worker = new NioEventLoopGroup();
            Bootstrap boot = new Bootstrap();
            boot.group(worker)
                    .channel(NioSocketChannel.class)
                    .remoteAddress("192.168.1.143", 9090)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            System.out.println("初始化client");
                            ChannelPipeline p = sc.pipeline();
                            p.addLast(new MyInbound());
                        }
                    });


            ChannelFuture conn = boot.connect().sync();

            Channel client = conn.channel();
            System.out.println(client);

            ByteBuf byteBuf = Unpooled.copiedBuffer("hello world".getBytes());
            client.writeAndFlush(byteBuf).sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
